/*
 * $Header: $
 * $Id$
 *
 * Copyright (c) 2011, Michael DePhillips. All Rights Reserved.
 * This code may not be used without the express written permission
 * of the copyright holder, Michael DePhillips.
 */
package com.facbook.bannermaker.android.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.facebook.bannermaker.android.HelpActivity;
import com.facebook.bannermaker.android.LoginActivity;
import com.facebook.bannermaker.android.R;
import com.facebook.bannermaker.android.activity.makebanner.MakeBannerMemActivity;
import com.facebook.bannermaker.android.activity.makebanner.MakeBannerTutorialActivity;
import com.facebook.bannermaker.android.activity.template.MakeBannerTemplateActivity;
import com.facebook.bannermaker.android.util.AppConstants;

/**
 * @author Michael DePhillips
 */
public class ActivityUtil {

    /**
     * Display error dialog
     * 
     * @param activity
     * @param message
     * @return
     */
    public static AlertDialog getAlertOkDialogue(Activity activity, String message) {

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(R.string.label_error_dialogue);
        builder.setMessage(message);
        builder.setCancelable(false).setNegativeButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
            // @Override
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        AlertDialog alert = builder.create();

        return alert;
    }

    /**
     * Display error dialog
     * 
     * @param activity
     * @param message
     * @return
     */
    public static AlertDialog getAlertOkFinishDialogue(final Activity activity, String message) {

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("Action Complete");
        builder.setMessage(message);
        builder.setCancelable(false).setNegativeButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
            //  @Override
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
                activity.finish();
            }
        });
        AlertDialog alert = builder.create();

        return alert;
    }

    /**
     * Load the activity to make a banner from a URL
     * @param activity
     */
    public static void loadMakeBannerFromMemoryActivity(Activity activity, String fbUid, String filePath) {

        Bundle bundle = new Bundle();
        bundle.putString(AppConstants.JSON_ME_ID, fbUid);
        bundle.putString(AppConstants.IMAGE_FILEPATH, filePath);
        Intent intent = new Intent(activity.getApplicationContext(), MakeBannerMemActivity.class);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }
    
    /**
     * Load the activity to show tutorial for making banner
     * @param activity
     */
    public static void loadMakeBannerTutorialActivity(Activity activity, String fbUid, String filePath) {

        Bundle bundle = new Bundle();
        bundle.putString(AppConstants.JSON_ME_ID, fbUid);
        bundle.putString(AppConstants.IMAGE_FILEPATH, filePath);
        Intent intent = new Intent(activity.getApplicationContext(), MakeBannerTutorialActivity.class);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }
    
    /**
     * Load the activity to make a banner from a template
     * @param activity
     */
    public static void loadMakeBannerFromTemplateActivity(Activity activity, String fbUid) {
        Intent intent = new Intent(activity.getApplicationContext(), MakeBannerTemplateActivity.class);
        activity.startActivity(intent);
    }
    
    /**
     * Load the login activity home screen
     * @param activity
     */
    public static void loadLoginActivity(Activity activity) {
        Intent intent = new Intent(activity.getApplicationContext(), LoginActivity.class);
        activity.startActivity(intent);
    }
    
    /**
     * Load the activity to show help info
     * @param activity
     */
    public static void loadHelpActivity(Activity activity) {
        Intent intent = new Intent(activity.getApplicationContext(), HelpActivity.class);
        activity.startActivity(intent);
    }
    
    public static boolean createGlobalOptionsMenu(Activity activity, Menu menu) {
        MenuInflater inflater = activity.getMenuInflater();
        inflater.inflate(R.menu.global_menu, menu);

        return true;
    }
    
    
    public static boolean createBannerMakerOptionsMenu(Activity activity, Menu menu) {
        MenuInflater inflater = activity.getMenuInflater();
        inflater.inflate(R.menu.banner_maker_menu, menu);
        return true;
    }

    /**
     * Control flow for options menu selection
     */
    public static boolean onGlobalOptionsItemSelected(Activity activity, MenuItem item) {
        if (item.getItemId() == R.id.menu_help) {
            loadHelpActivity(activity);
        }
        return true;
    }
}
